<template>
	<view class="group-view">
		<view class="line-view"></view>
		<view class="logo-group-view">
			<image :src="baseImageUrl+'/login/logo.png'"></image>
		</view>
		
		<button class='login-btn-view' @click="userLogin()">
			<text>快捷登录</text>
		</button>
		
		<text @click="phoneLoginClick()" class="phone-login-view">手机号快捷登录</text>
		
		<view style="display: flex;margin-top: 52rpx;margin-bottom: 64rpx;">
			<view @click="checkAgreenClick()" style="display: flex;align-items: center;">
				<image class="agreement-image" :src="isCheck?'/static/login/ic_select_check.png':'/static/login/ic_select_normal.png'"/>
			</view>
			<view class="agreement-view">我已阅读并同意
				<text @click="registerPolicyClick()" class="agreement-text">《抖鑫用户使用协议》</text>
				<text @click="privacyPolicyClick()" class="agreement-text">《隐私政策》</text>
			</view>
		</view>
	</view>
</template>

<script>
	import configUrls from '../../common/config.js';
	export default {
	    data() {
	        return {
				baseImageUrl: configUrls.baseImageUrl,
				iStatusBarHeight: 0,
				navigationBarHeight: 0,
				navHeight: 0,
				baseImageUrl: configUrls.baseImageUrl,
				isCheck: false,
				wxCode: '',
				wxUnionId: '',
				wxOpenId: '',
	        }
	    },  
		onReady() {
			this.iStatusBarHeight = uni.getSystemInfoSync().statusBarHeight;	
			const customHeight = wx.getMenuButtonBoundingClientRect();
			this.navigationBarHeight = customHeight.height + (customHeight.top - this.iStatusBarHeight) * 2;
		},
		onLoad(options)  {
			let that = this;
			uni.login({
			  	scopes:'auth_user',
			  	success: (loginInfo) => {
			  	    console.log("loginInfo:" + JSON.stringify(loginInfo))
					that.wxCode = loginInfo.code
				}
			})
	    },
		methods:{
			userLogin() {
				if (!this.isCheck) {
					uni.showToast({
						title: "请先勾选同意用户协议",
						icon: 'none',
						duration: 2000
					});
					return
				}
				if (this.wxUnionId) {
					this.toRegister();
					return
				}
				let that = this
				this.$Request.postJsonLoadView(configUrls.miniLogin, {
					code: that.wxCode
				}).then(res => {
					console.log("登录=======>"+JSON.stringify(res));
					if (res.code == "10000"){
						let loinMsgDict = res.data.token;
						if (loinMsgDict) {
							uni.setStorageSync("accessToken", loinMsgDict.token);
							uni.setStorageSync("userId", loinMsgDict.userId);
							uni.setStorageSync("expireTime", loinMsgDict.expireTime);
							uni.setStorageSync("isLogin", true);
							uni.switchTab({
								url: '/pages/main/main'
							})
						} else {
							let wxResult = res.data.wxResult;
							that.wxUnionId = wxResult.unionid;
							that.wxOpenId = wxResult.openid;
							that.toRegister();
						}
					} else{
						uni.showToast({
							title: res.msg,
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			toRegister() {
				console.log("进入注册=======>" + this.wxUnionId);
				let loginParams = {
					unionId: this.wxUnionId,
					openId: this.wxOpenId,
					isRegister: true
				}
				uni.navigateTo({
					url: '/pages/login/login?loginParams='+JSON.stringify(loginParams)
				})
			},
			phoneLoginClick() {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			},
			checkAgreenClick(){
				if (this.isCheck) {
					this.isCheck = false;
				} else {
					this.isCheck = true;
				}
			},
			registerPolicyClick() {
				uni.navigateTo({
					url: '/pages/index/h5Webview?titleStr=用户协议&url='+ encodeURIComponent(JSON.stringify(configUrls.registerPolicy))
				});
			},
			privacyPolicyClick() {
				uni.navigateTo({
					url: '/pages/index/h5Webview?titleStr=隐私政策&url='+ encodeURIComponent(JSON.stringify(configUrls.privacyPolicy))
				});
			}
		}
	}
</script>

<style>
	@import url("../../css/login/wxlogin.css");
</style>
